<?php
if (!isset($model))
    $model = new BDynamicFormModel ();
$categoryNameField = 'media_name_buyer_category';
$categoryAliasField = 'media_alias_buyer_category';
$model->addField($categoryNameField);
$model->addField($categoryAliasField);

$attributeNameField = 'media_name_buyerCategory_attribute';
$attributeAliasField = 'media_alias_buyerCategory_attribute';
$attributeTypeField = 'media_type_buyerCategory_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$buyerCategoryAttributeView = array();

if ($categoryId > 0) {
    $buyerCategoryTable = new BPSheet('buyerCategory');
    $buyerCategory = $buyerCategoryTable->getListById($categoryId);

    $buyerCategoryAttributeTable = new BPSheet('buyerCategoryAttribute', $buyerCategory);
    $categoryAttributeView = $buyerCategoryAttributeTable->getView();

    /**
     * below code will populate attribute for product
     */
    foreach ($categoryAttributeView as $id => $categoryAttribute) {
        $nameItem = $categoryAttribute->getItem('name');
        $typeItem = $categoryAttribute->getItem('type');
        if ($nameItem instanceof BTreeItem) {
            $name = $nameItem->getValue();
            $attributeField = 'media_' . $name . '_buyer_category';
            $model->addField($attributeField);
            //get value of attribute in product 
            $attributeItem = $buyerCategory->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
        }
        if ($typeItem instanceof BTreeItem) {
            $type = $nameItem->getValue();
            $attributeField = 'media_' . $type . '_buyer_category';
            $model->addField($attributeField);
        }
    }

    $categoryNameItem = $buyerCategory->getItem('name');
    if ($categoryNameItem instanceof BTreeItem) {
        $categoryName = $categoryNameItem->getValue();
        $model->$categoryNameField = $categoryName;
    }
    $categoryAliasItem = $buyerCategory->getItem('alias');
    if ($categoryAliasItem instanceof BTreeItem) {
        $categoryAlias = $categoryAliasItem->getValue();
        $model->$categoryAliasField = $categoryAlias;
    }
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addBuyerCategoryForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('saveBuyerCategory', NULL, array(
    'eventId'=>$eventId,
    'categoryId' => $categoryId));
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $categoryId; ?>() {

        var formData = new FormData($("#addBuyerCategoryForm")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<legend>
    <h4>
        <?php echo $control->getAjaxLink(array('name' => '活动列表', 'id' => 'showEventList')); ?>\
        <?php echo $control->getAjaxLink(array('name' => '分类列表', 'id' => 'showBuyerCategoryList'),array('eventId'=>$eventId)); ?>\
        <?php echo '设置分类'; ?>
    </h4>
</legend>
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $typeList = $app->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addBuyerCategoryAttribute'), array(
            'htmlOptions' => array('class' => 'btn'),
            'categoryId' => $categoryId,
            'eventId'=>$eventId,
            'attributeId' => 0
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '分类名称'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $categoryNameField, array('placeholder' => 'category name' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr><td> <?php echo '分类别名'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $categoryAliasField, array('placeholder' => 'category alias' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>               

        <?php
        if ($categoryId > 0) {
            foreach ($categoryAttributeView as $categoryAttribute) {
                $attributeId = $categoryAttribute->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $categoryAttribute->getItem('name');
                    $typeItem = $categoryAttribute->getItem('type');
                    $aliasItem = $categoryAttribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . '_buyer_category';
                        $attributeItem = $buyerCategory->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        }
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $app->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deleteBuyerCategory'), array(
                            'htmlOptions' => array('class' => 'btn'),
                            'categoryId' => $categoryId,
                            'attributeId' => $attributeId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:send<?php echo $categoryId; ?>()"><?php echo '保存配置信息' ?></button>